﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using NewLife;
using NewLife.Data;
using XCode;
using XCode.Cache;
using XCode.Configuration;
using XCode.DataAccessLayer;

namespace EHO.AutoUpdate.Server.Database;

/// <summary>程序信息</summary>
[Serializable]
[DataObject]
[Description("程序信息")]
[BindIndex("IU_App_AppKey", true, "AppKey")]
[BindTable("App", Description = "程序信息", ConnName = "AutoUpdate", DbType = DatabaseType.SqlServer)]
public partial class App
{
    #region 属性
    private Int64 _Id;
    /// <summary>主键Id</summary>
    [DisplayName("主键Id")]
    [Description("主键Id")]
    [DataObjectField(true, false, false, 0)]
    [BindColumn("Id", "主键Id", "", DataScale = "time")]
    public Int64 Id { get => _Id; set { if (OnPropertyChanging("Id", value)) { _Id = value; OnPropertyChanged("Id"); } } }

    private String _Name;
    /// <summary>名称</summary>
    [DisplayName("名称")]
    [Description("名称")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("Name", "名称", "", Master = true)]
    public String Name { get => _Name; set { if (OnPropertyChanging("Name", value)) { _Name = value; OnPropertyChanged("Name"); } } }

    private String _AppKey;
    /// <summary>程序Key</summary>
    [DisplayName("程序Key")]
    [Description("程序Key")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("AppKey", "程序Key", "")]
    public String AppKey { get => _AppKey; set { if (OnPropertyChanging("AppKey", value)) { _AppKey = value; OnPropertyChanged("AppKey"); } } }

    private String _Secret;
    /// <summary>程序Id</summary>
    [DisplayName("程序Id")]
    [Description("程序Id")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("Secret", "程序Id", "")]
    public String Secret { get => _Secret; set { if (OnPropertyChanging("Secret", value)) { _Secret = value; OnPropertyChanged("Secret"); } } }

    private String _Description;
    /// <summary>备注</summary>
    [DisplayName("备注")]
    [Description("备注")]
    [DataObjectField(false, false, true, 200)]
    [BindColumn("Description", "备注", "")]
    public String Description { get => _Description; set { if (OnPropertyChanging("Description", value)) { _Description = value; OnPropertyChanged("Description"); } } }

    private DateTime _CreateTime;
    /// <summary>创建日期</summary>
    [DisplayName("创建日期")]
    [Description("创建日期")]
    [DataObjectField(false, false, true, 0)]
    [BindColumn("CreateTime", "创建日期", "", Precision = 0, Scale = 3)]
    public DateTime CreateTime { get => _CreateTime; set { if (OnPropertyChanging("CreateTime", value)) { _CreateTime = value; OnPropertyChanged("CreateTime"); } } }

    private Int64 _CreateUserID;
    /// <summary>创建用户主键</summary>
    [DisplayName("创建用户主键")]
    [Description("创建用户主键")]
    [DataObjectField(false, false, false, 0)]
    [BindColumn("CreateUserID", "创建用户主键", "")]
    public Int64 CreateUserID { get => _CreateUserID; set { if (OnPropertyChanging("CreateUserID", value)) { _CreateUserID = value; OnPropertyChanged("CreateUserID"); } } }

    private String _CreateUser;
    /// <summary>创建用户</summary>
    [DisplayName("创建用户")]
    [Description("创建用户")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("CreateUser", "创建用户", "")]
    public String CreateUser { get => _CreateUser; set { if (OnPropertyChanging("CreateUser", value)) { _CreateUser = value; OnPropertyChanged("CreateUser"); } } }

    private String _CreateIP;
    /// <summary>创建地址</summary>
    [DisplayName("创建地址")]
    [Description("创建地址")]
    [DataObjectField(false, false, true, 50)]
    [BindColumn("CreateIP", "创建地址", "")]
    public String CreateIP { get => _CreateIP; set { if (OnPropertyChanging("CreateIP", value)) { _CreateIP = value; OnPropertyChanged("CreateIP"); } } }
    #endregion

    #region 获取/设置 字段值
    /// <summary>获取/设置 字段值</summary>
    /// <param name="name">字段名</param>
    /// <returns></returns>
    public override Object this[String name]
    {
        get => name switch
        {
            "Id" => _Id,
            "Name" => _Name,
            "AppKey" => _AppKey,
            "Secret" => _Secret,
            "Description" => _Description,
            "CreateTime" => _CreateTime,
            "CreateUserID" => _CreateUserID,
            "CreateUser" => _CreateUser,
            "CreateIP" => _CreateIP,
            _ => base[name]
        };
        set
        {
            switch (name)
            {
                case "Id": _Id = value.ToLong(); break;
                case "Name": _Name = Convert.ToString(value); break;
                case "AppKey": _AppKey = Convert.ToString(value); break;
                case "Secret": _Secret = Convert.ToString(value); break;
                case "Description": _Description = Convert.ToString(value); break;
                case "CreateTime": _CreateTime = value.ToDateTime(); break;
                case "CreateUserID": _CreateUserID = value.ToLong(); break;
                case "CreateUser": _CreateUser = Convert.ToString(value); break;
                case "CreateIP": _CreateIP = Convert.ToString(value); break;
                default: base[name] = value; break;
            }
        }
    }
    #endregion

    #region 关联映射
    #endregion

    #region 扩展查询
    /// <summary>根据主键Id查找</summary>
    /// <param name="id">主键Id</param>
    /// <returns>实体对象</returns>
    public static App FindById(Int64 id)
    {
        if (id < 0) return null;

        return Find(_.Id == id);
    }

    /// <summary>根据程序Key查找</summary>
    /// <param name="appKey">程序Key</param>
    /// <returns>实体对象</returns>
    public static App FindByAppKey(String appKey)
    {
        if (appKey.IsNullOrEmpty()) return null;

        return Find(_.AppKey == appKey);
    }
    #endregion

    #region 数据清理
    /// <summary>清理指定时间段内的数据</summary>
    /// <param name="start">开始时间。未指定时清理小于指定时间的所有数据</param>
    /// <param name="end">结束时间</param>
    /// <returns>清理行数</returns>
    public static Int32 DeleteWith(DateTime start, DateTime end)
    {
        return Delete(_.Id.Between(start, end, Meta.Factory.Snow));
    }
    #endregion

    #region 字段名
    /// <summary>取得程序信息字段信息的快捷方式</summary>
    public partial class _
    {
        /// <summary>主键Id</summary>
        public static readonly Field Id = FindByName("Id");

        /// <summary>名称</summary>
        public static readonly Field Name = FindByName("Name");

        /// <summary>程序Key</summary>
        public static readonly Field AppKey = FindByName("AppKey");

        /// <summary>程序Id</summary>
        public static readonly Field Secret = FindByName("Secret");

        /// <summary>备注</summary>
        public static readonly Field Description = FindByName("Description");

        /// <summary>创建日期</summary>
        public static readonly Field CreateTime = FindByName("CreateTime");

        /// <summary>创建用户主键</summary>
        public static readonly Field CreateUserID = FindByName("CreateUserID");

        /// <summary>创建用户</summary>
        public static readonly Field CreateUser = FindByName("CreateUser");

        /// <summary>创建地址</summary>
        public static readonly Field CreateIP = FindByName("CreateIP");

        static Field FindByName(String name) => Meta.Table.FindByName(name);
    }

    /// <summary>取得程序信息字段名称的快捷方式</summary>
    public partial class __
    {
        /// <summary>主键Id</summary>
        public const String Id = "Id";

        /// <summary>名称</summary>
        public const String Name = "Name";

        /// <summary>程序Key</summary>
        public const String AppKey = "AppKey";

        /// <summary>程序Id</summary>
        public const String Secret = "Secret";

        /// <summary>备注</summary>
        public const String Description = "Description";

        /// <summary>创建日期</summary>
        public const String CreateTime = "CreateTime";

        /// <summary>创建用户主键</summary>
        public const String CreateUserID = "CreateUserID";

        /// <summary>创建用户</summary>
        public const String CreateUser = "CreateUser";

        /// <summary>创建地址</summary>
        public const String CreateIP = "CreateIP";
    }
    #endregion
}
